#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int cJSON_SetKeyValue(cJSON *obj, const char *key, const char *value)
{
    cJSON *tmp;
    cJSON *p;
    int i, j;
    int arraySize;
    string strtmp;
    tmp = obj;
    if (!tmp)
        return -1;
    p = tmp;
    tmp = tmp->child;
    while (1) {
        if (!tmp) {
            if (p) {
                switch (p->type) {
                    case cJSON_Number:
                        break;
                    case cJSON_String:
                        break;
                }
            }
            break;
        }

        if (tmp->type == cJSON_Array) {
            arraySize = cJSON_GetArraySize(tmp);
            for (i = 0; i < arraySize; i++) {
                p = cJSON_GetArrayItem(tmp, i);
                    if (p) {
                        if (p->type == cJSON_Object)
                            cJSON_SetKeyValue(p, key, value);
                        else {
                            cJSON_SetKeyValue(p, key, value);
                        }
                    } else {

                    }
                }
            tmp = tmp->next;
        } else if (tmp->string && strcmp(tmp->string, "null")){
			if (strcmp(tmp->string, key) == 0) {
				cout << "find it" << endl;
				switch (tmp->type) {
                case cJSON_Number:
                    tmp->valueint = atoi(value);
					tmp->valuedouble = atof(value);
                    break;
                case cJSON_String:
					tmp->valuestring = (char*)realloc((void*)tmp->valuestring, strlen(value) + 1);
                    memset(tmp->valuestring, 0, strlen(value) + 1);
                    memcpy(tmp->valuestring, value, strlen(value));
					break;
                default:
                    break;
				}
			}
            
            tmp = tmp->next;
        } else {
            switch (tmp->type) {
                case cJSON_Number:
                    
					getline(cin, strtmp);
                    tmp->valueint  = atoi(strtmp.c_str());
                    tmp->valuedouble = atof(strtmp.c_str());
                    cout << tmp->valueint << "," << atoi(strtmp.c_str()) << endl;
                    break;
                case cJSON_String:
					
                    getline(cin, strtmp);
                    if (strtmp.size()) {
                        tmp->valuestring = (char*)realloc((void*)tmp->valuestring, strtmp.size() + 1);
                        memset(tmp->valuestring, 0, strtmp.size() + 1);
                        memcpy(tmp->valuestring, strtmp.c_str(), strtmp.size());
                    }
                    cout << tmp->valuestring << "," << strtmp << endl;
                    break;
            }
            tmp = tmp->next;
        }
    }

    return 0;
}

int cJSON_SetAllItem(cJSON *obj)
{
    cJSON *tmp;
    cJSON *p;
    int i, j;
    int arraySize;

    tmp = obj;
    if (!tmp)
        return -1;
    p = tmp;
    tmp = tmp->child;
    while (1) {
        if (!tmp) {
            if (p) {
                switch (p->type) {
                    case cJSON_Number:
                        //printf("value:%d\n", p->valueint);
						cin >> p->valueint;
                        break;
                    case cJSON_String:
                        //printf("value:%s\n", p->valuestring);
						cin >> p->valuestring;
                        break;
                }
            }
            break;
        }

        if (tmp->type == cJSON_Array) {
            
            arraySize = cJSON_GetArraySize(tmp);
            for (i = 0; i < arraySize; i++) {
                p = cJSON_GetArrayItem(tmp, i);
                    if (p) {
						printf("array[%d]:%s\n", i, tmp->string);
                        if (p->type == cJSON_Object)
                            cJSON_SetAllItem(p);
                        else {
                            cJSON_SetAllItem(p);
                        }
                    } else {
                        //printf("what?\n");
                    }
                }
            tmp = tmp->next;
        } else if (tmp->string && strcmp(tmp->string, "null")){
            printf("%s:", tmp->string);
            switch (tmp->type) {
                case cJSON_Number:
                    //printf("value:%d\n", tmp->valueint);
					cin >> tmp->valueint;
                    break;
                case cJSON_String:
                    //printf("value:%s\n", tmp->valuestring);
					cin >> tmp->valuestring;
                    break;
                default:
                    //printf("value:\n");
                    break;
            }
            tmp = tmp->next;
        } else {
            switch (tmp->type) {
                case cJSON_Number:
                    //printf("number:%lf", tmp->valuedouble);
					cin >> tmp->valuedouble;
                    break;
                case cJSON_String:
                    //printf("string:%s\n", tmp->valuestring);
					cin >> tmp->valuestring;
                    break;
            
            }
            tmp = tmp->next;
            //n++;
        }

    }

    return 0;
}
#if 0
int cJSON_ShowByList(cJSON *obj)
{
    cJSON *tmp;
    cJSON *p;
    int i, j;
    int arraySize;

    tmp = obj;
    if (!tmp)
        return -1;
    p = tmp;
    tmp = tmp->child;
    while (1) {
        if (!tmp) {
            if (p) {
                switch (p->type) {
                    case cJSON_Number:
                        printf("value:%d\n", p->valueint);
                        break;
                    case cJSON_String:
                        printf("value:%s\n", p->valuestring);
                        break;
                }
            }
            break;
        }

        if (tmp->type == cJSON_Array) {
            printf("array:%s, size:%d\n", tmp->string, cJSON_GetArraySize(tmp));
            arraySize = cJSON_GetArraySize(tmp);
            for (i = 0; i < arraySize; i++) {
                p = cJSON_GetArrayItem(tmp, i);
                    if (p) {
                        if (p->type == cJSON_Object)
                            //cJSON_Show(p);
                        else {
                            //cJSON_Show(p);
                        }
                    } else {
                        printf("what?\n");
                    }
                }
            tmp = tmp->next;
        } else if (tmp->string && strcmp(tmp->string, "null")){
            printf("key:%s\t", tmp->string);
            switch (tmp->type) {
                case cJSON_Number:
                    printf("value:%d\n", tmp->valueint);
                    break;
                case cJSON_String:
                    printf("value:%s\n", tmp->valuestring);
                    break;
                default:
                    printf("value:\n");
                    break;
            }
            tmp = tmp->next;
        } else {
            switch (tmp->type) {
                case cJSON_Number:

                    printf("number:%lf", tmp->valuedouble);
                    break;
                case cJSON_String:
                    printf("string:%s\n", tmp->valuestring);
                    break;
            
            }
            tmp = tmp->next;	//	当前对象中遍历
            //n++;
        }

    }

    return 0;
}
#endif
int cJSON_Show(cJSON *obj)
{
    cJSON *tmp;
    cJSON *p;
    int i, j;
    int arraySize;

    tmp = obj;
    if (!tmp)
        return -1;
    p = tmp;
    tmp = tmp->child;
    while (1) {
        if (!tmp) {
            if (p) {
                switch (p->type) {
                    case cJSON_Number:
                        printf("value:%d\n", p->valueint);
                        break;
                    case cJSON_String:
                        printf("value:%s\n", p->valuestring);
                        break;
                }
            }
            break;
        }

        if (tmp->type == cJSON_Array) {
            printf("array:%s, size:%d\n", tmp->string, cJSON_GetArraySize(tmp));
            arraySize = cJSON_GetArraySize(tmp);
            for (i = 0; i < arraySize; i++) {
                p = cJSON_GetArrayItem(tmp, i);
                    if (p) {
                        if (p->type == cJSON_Object)
                            cJSON_Show(p);
                        else {
                            cJSON_Show(p);
                        }
                    } else {
                        printf("what?\n");
                    }
                }
            tmp = tmp->next;
        } else if (tmp->string && strcmp(tmp->string, "null")){
            printf("obj:%s\t", tmp->string);
            switch (tmp->type) {
                case cJSON_Number:
                    printf("value:%d\n", tmp->valueint);
                    break;
                case cJSON_String:
                    printf("value:%s\n", tmp->valuestring);
                    break;
                default:
                    printf("value:\n");
                    break;
            }
            tmp = tmp->next;
        } else {
            switch (tmp->type) {
                case cJSON_Number:

                    printf("number:%lf", tmp->valuedouble);
                    break;
                case cJSON_String:
                    printf("string:%s\n", tmp->valuestring);
                    break;
            
            }
            tmp = tmp->next;
            //n++;
        }

    }

    return 0;
}

int cJSON_ChangeToTable(cJSON *obj, vector<string> &m_v, int &rowsize)
{
    cJSON *tmp;
    cJSON *pp, *cp, *p;
    int i, j;
    int arraySize;
	

    tmp = obj;
    if (!tmp)
        return -1;
    p = tmp;
    tmp = tmp->child;
    if (1) {
        if (!tmp) {
            return -1;
        }
		printf("string:%s\ntype:%d\n", tmp->string, tmp->type);
        if (tmp->type == cJSON_Array) {
			{
				printf("get theme\n");
				arraySize = cJSON_GetArraySize(tmp);
				for (i = 0, pp = cJSON_GetArrayItem(tmp, i);  i < 1; i++) {
					printf("string:%s, type:%d\n", pp->string, pp->type);
					if (pp->type != cJSON_Object)
						break;
					cp = pp->child;
					#if 1
					while (cp) {
						//cp = cp->child;
						//cp&&printf("%s ", cp->string);
						if (cp && cp->string && !(cp->type == cJSON_Array)) {
							//switch() (cp->type) 
							printf("%s ", cp->string);
							m_v.push_back(cp->string);
						}
						cp = cp->next;
					}
					#endif
				}
				//m_v.push_back(v_t);
				printf("\n");
				//break;
			}
			{
				printf("get value\n");
				rowsize = arraySize = cJSON_GetArraySize(tmp);
				rowsize += 1;
				for (i = 0;  i < arraySize; i++) {
					pp = cJSON_GetArrayItem(tmp, i);
					printf("string:%s, type:%d\n", pp->string, pp->type);
					if (pp->type != cJSON_Object)
						continue;
					cp = pp->child;
					#if 1
					//v_t.clear();
					while (cp) {
						stringstream ss;
						//cp = cp->child;
						//cp&&printf("%s ", cp->string);
						//printf("%s ", cp->string);
						if (cp && cp->string) {
							//switch() (cp->type) 
							printf("%s ", cp->string);
							switch (cp->type) {
								case cJSON_Number:
									printf("%d ", cp->valueint);
									ss.clear();
									ss << cp->valueint;
									m_v.push_back(ss.str());
									break;
								case cJSON_String:
									printf("%s ", cp->valuestring);
									m_v.push_back(string(cp->valuestring));
									break;
							}
							
						}
						printf("\n");
						cp = cp->next;
					}
					//m_v.push_back(v_t);
					#endif
				}
				return 0;
			}
        } else {	//	获取object
			if (tmp->type == cJSON_Object) {
				printf("object!\n");
				tmp = tmp->child;
				if (!tmp)
					return -1;
			}
					
			{
				pp = tmp;
				printf("get theme\n");

				printf("string:%s, type:%d\n", pp->string, pp->type);
				cp = pp;
				#if 1
				while (cp) {
					//cp = cp->child;
					//cp&&printf("%s ", cp->string);
					if (cp && cp->string && !(cp->type == cJSON_Array)) {
						//switch() (cp->type) 
						printf("%s ", cp->string);
						m_v.push_back(cp->string);
					} else {
						cout << "ignore one" << endl;
						//break;
					}
					cp = cp->next;
				}
				#endif
				//m_v.push_back(v_t);
				printf("\n");
				//break;
			}
			{
				printf("get value\n");
				rowsize = 1;
				rowsize += 1;
				pp = tmp;
				cp = pp;
				//v_t.clear();
				while (cp) {
					//cp = pp->child;
					stringstream ss;
					if (cp && cp->string && !(cp->type == cJSON_Array)) {
						printf("%s ", cp->string);
						switch (cp->type) {
							case cJSON_Number:
								printf("%d ", cp->valueint);
								ss.clear();
								ss << cp->valueint;
								m_v.push_back(ss.str());
								break;
							case cJSON_String:
								printf("%s ", cp->valuestring);
								m_v.push_back(string(cp->valuestring));
								break;
						}
					} else {
						cout << "ignore one" << endl;
						//break;
					}
					printf("\n");
					cp = cp->next;
				}
			}
		}
	}
    return 0;
}

int cJSON_ShowByTree(cJSON *obj, int n)
{
    cJSON *tmp;
    cJSON *p;
	int i,j;
    int arraySize;
	int m;

    tmp = obj;
    if (!tmp)
        return -1;
    p = tmp;
    tmp = tmp->child;
    while (1) {
        if (!tmp) {
            if (p) {
				
                switch (p->type) {
                    case cJSON_Number:
						m = n - 3;if (m > 0) {while ((m--)) printf(" ");}
						m = n - 3 - 1;m&&printf("|");if (m > 0){while (m--) printf("_");}
                        printf("%-10d\n", p->valueint);
                        break;
                    case cJSON_String:
						m = n - 3;if (m > 0) {while ((m--)) printf(" ");}
						m = n - 3 - 1;m&&printf("|");if (m > 0){while (m--) printf("_");}
                        printf("%-10s\n", p->valuestring);
                        break;
                }
            }
            break;
        }

        if (tmp->type == cJSON_Array) {
			m = n - 3;if (m > 0) {while ((m--)) printf(" ");}
			m = n - 3 - 1;m&&printf("|");if (m > 0){while (m--) printf("_");}
            printf("array:%s, size:%d\n", tmp->string, cJSON_GetArraySize(tmp));
            arraySize = cJSON_GetArraySize(tmp);
            for (i = 0, m = n; i < arraySize; i++) {
                p = cJSON_GetArrayItem(tmp, i);
                    if (p) {
                        if (p->type == cJSON_Object)
                            cJSON_ShowByTree(p, m + 4);
                        else {
                            cJSON_ShowByTree(p, m + 4);
                        }
                    } else {
                        printf("what?\n");
                    }
                }
            tmp = tmp->next;
        } else if (tmp->string && strcmp(tmp->string, "null")){
			m = n - 3;if (m > 0) {while ((m--)) printf(" ");}
			m = n - 3 - 1;m&&printf("|");if (m > 0){while (m--) printf("_");}
            printf("%1s:", tmp->string);
            switch (tmp->type) {
                case cJSON_Number:
					
                    printf("%-10d\n", tmp->valueint);
                    break;
                case cJSON_String:
                    printf("%-10s\n", tmp->valuestring);
                    break;
                default:
                    printf("\n");
                    break;
            }
            tmp = tmp->next;
        } else {
            switch (tmp->type) {
                case cJSON_Number:
                    printf("%-10f\n", tmp->valuedouble);
                    break;
                case cJSON_String:
                    printf("%-10s\n", tmp->valuestring);
                    break;
            
            }
            tmp = tmp->next;
            //n++;
        }

    }

    return 0;
}

int cJSON_SetObjectItemTravel(cJSON *obj)
{
	cJSON *tmp;
    cJSON *pp, *cp, *p;
    int i, j;
    int arraySize;
	string newValue;
	

    tmp = obj;
    if (!tmp)
        return -1;
    pp = tmp;
    cp = tmp->child;
    while (1) {
        if (!cp) {
            return -1;
        }
		
		if (cp->type == cJSON_Object) {
			cp = cp->child;
		}
		
		if (cp) {
			if (cp->string && !(cp->type == cJSON_Array)) {
				printf("%s:", cp->string);
				switch (cp->type) {
					case cJSON_Number:
						cin >> cp->valueint;
						cp->valuedouble = cp->valueint;
						break;
					case cJSON_String:
						cin >> cp->valuestring;
						break;
				}
			} else {
				break;
			}
		}
		cp = cp->next;
	}
	return 0;
}

int main(int argc, const char *argv[])
{
	vector<string> m_v;
	int rowsize, colsize;
#if 0
    cJSON *mJson = cJSON_Parse(
					"{\"RouteTable\":	[{"
						"\"RouteNo\":	10,"
						"\"SpanGroupInNo\":	10,"
						"\"SpanGroupOutNo\":	10,"
						"\"Rule\":	[]"
					"}, {"
						"\"RouteNo\":	11,"
						"\"SpanGroupInNo\":	999,"
						"\"SpanGroupOutNo\":	1,"
						"\"Rule\":	[]"
					"}, {"
						"\"RouteNo\":	12,"
						"\"SpanGroupInNo\":	1,"
						"\"SpanGroupOutNo\":	999,"
						"\"Rule\":	[]"
					"}, {"
						"\"RouteNo\":	1021,"
						"\"SpanGroupInNo\":	10,"
						"\"SpanGroupOutNo\":	1,"
						"\"Rule\":	[]"
					"}]}"
	);
#endif
#if 1
       cJSON *mJson = cJSON_Parse("{\"refresh_interval\":60,"
                        "\"timeout\":12.5,"
						"\"arrayNumber\":[1,2,3,4,5,6],"
						"\"timeout1\":12.5,"
						"\"wife\":[\"li\",\"zhuo\",\"cong\"],"
						"\"timeout2\":12.5,"
                        "\"dynamic_loading\":["
                            "{"
                                "\"so_path\":\"plugins/User.so\", \"load\":false, \"version\":1,"
                                "\"cmd\":["
                                     "{\"cmd\":2001, \"class\":\"neb::CmdUserLogin\"},"
                                     "{\"cmd\":2003, \"class\":\"neb::CmdUserLogout\"}"
                                "],"
                                "\"module\":["
                                     "{\"path\":\"im/user/login\", \"class\":\"neb::ModuleLogin\"},"
                                     "{\"path\":\"im/user/logout\", \"class\":\"neb::ModuleLogout\"}"
                                "]"
                             "},"
                             "{"
                             "\"so_path\":\"plugins/ChatMsg.so\", \"load\":false, \"version\":1,"
                                 "\"cmd\":["
                                      "{\"cmd\":2001, \"class\":\"neb::CmdChat\"}"
                                 "],"
                             "\"module\":[]"
                             "}"
                        "]"
                    "}");
#endif
	//cJSON *mJson = cJSON_Parse("{\"groupInfo\":{\"groupName\":\"210.15\",\"lineType\":1,\"transportType\":0,\"mediaProtocol\":0,\"localRingtone\":0,\"faxMode\":2,\"faxRate\":1,\"mediaService\":0,\"userId\":-1,\"dtmf\":0,\"mediaType\":[0],\"suppressMode\":0,\"regulationMode\":0,\"gainDirection\":1,\"gainLevel\":5,\"send180\":0,\"send183\":2,\"sendingMode\":1,\"callMode\":0,\"interval\":1,\"mediaBinding\":[{\"MediacIsxNo\":0,\"MediacBrdNo\":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]}],\"sipResource\":[{\"cIsxNo\":0,\"cBrdNo\":0,\"index\":0,\"address\":\"10.10.18.132:5060\",\"resource\":[{\"usPort\":5060,\"ip\":\"10.10.210.15\",\"bMaster\":0}]}]}}");
    //cJSON_ShowByTree(mJson, 1);
	//cJSON_SetKeyValue(mJson, "timeout", "10");
	//printf("\n");
	//cJSON_SetAllItem(mJson);
	//printf("\n");
	//cJSON_ShowByTree(mJson);
#if 1
	printf("-----------------------------------\n");
    cJSON_ChangeToTable(mJson, m_v, rowsize);
	printf("-----------------------------------\n");

	printf("m_v:%d\n", m_v.size());
	//vector<string> v;
	colsize = m_v.size()/rowsize;
	printf("colsize:%d\n", colsize);
	for (int i = 0;i < m_v.size(); i++)
	{
		if (!((i)%colsize))
			printf("\n");
		cout.setf(ios::left); 
		cout.width(15);
		cout << m_v[i].data();
		cout.unsetf(ios::left);
	}
	printf("\n");
	#if 0
	printf("----------------cJSON_SetObjectItemTravel-------------------\n");
	cJSON_SetObjectItemTravel(mJson);
	printf("----------------cJSON_SetObjectItemTravel-------------------\n");
	m_v.clear();
	cJSON_ChangeToTable(mJson, m_v, rowsize);
	printf("-----------------------------------\n");

	printf("m_v:%d\n", m_v.size());
	//vector<string> v;
	colsize = m_v.size()/rowsize;
	printf("colsize:%d\n", colsize);
	for (int i = 0;i < m_v.size(); i++)
	{
		if (!((i)%colsize))
			printf("\n");
		cout.setf(ios::left); 
		cout.width(15);
		cout << m_v[i].data();
		cout.unsetf(ios::left);
	}
	#endif
#endif
	printf("\n");
	return 0;
}
